// import { login, logout, getInfo } from '@/api/user'
// import { getToken, setToken, removeToken } from '@/utils/auth'
// import { resetRouter } from '@/router'

import { getUserInfApi, loginApi } from '@/api/user'
import { getToken, removeToken, setToken } from '@/utils/auth'

const state = () => ({
  token: getToken(),
  userInfo: {}
})

const mutations = {
  // 设置token
  setToken(state, payload) {
    state.token = payload
    setToken(payload)
  },
  // 删除token
  removeToken(state) {
    state.token = null
    removeToken()
  },
  // 设置用户信息
  setUserInfo(state, payload) {
    state.userInfo = payload
  },
  // 删除用户信息
  removeUserInfo(state) {
    state.userInfo = {}
  }

}

const actions = {
  // 登录
  async  loginAction({ commit }, payload) {
    console.log(payload)
    const res = await loginApi(payload)
    commit('setToken', res.token)
  },

  // 获取用户信息
  async getUserInfoAction({ commit }) {
    const { userInfo } = await getUserInfApi()
    commit('setUserInfo', userInfo)
  },
  // 退出
  async logoutAction({ commit }) {
    commit('removeToken')
    commit('removeUserInfo')
  }

}

export default {
  namespaced: true,
  state,
  mutations,
  actions
}

